package org.ejde.sciencelogs;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 * This class contains the attributes and behaviors for the
 * XMPP stat panel.
 *  
 * @author Eric Martin
 * @version 0.1
 */
public class smackStatPanel extends JPanel implements ActionListener {

	// JComponents in the panel
	private JLabel username;
	private JLabel messages;
	private JButton loginout;
	
	// The XMPP-client
	private smackClient smack;
	
	// The number of sent messages
	private int sentMessages;
	
	// The configuration information
	private iniParser ini;

	/**
	 * Constructor method.
	 *
	 * @param iniparser configuration information to use with this panel
	 * @param smackclient the XMPP client to use with this client
	 */
	smackStatPanel(iniParser iniparser, smackClient smackclient) {

		// Set the border
		Border titledborder = BorderFactory.createTitledBorder("Server");
		this.setBorder(titledborder);

		// Copy the configuration data to the internal
		// configuration object.
		this.ini = iniparser;

		// Copy the XMPP client object to the internal
		// XMPP client object.
		this.smack = smackclient;
		
		// Create the JComponents.
		this.username = new JLabel("not logged in");
		this.messages = new JLabel("0");
		this.loginout = new JButton("Login");

		// Set the initial text to be used on the message
		// counter JLabel and Login/Logout JButton. 
		this.updateLogInOut();
		this.updateUserName();

		// Initalize the JComponents
		this.setLayout(new GridLayout(0, 2));
		this.add(new JLabel("Username:  ", JLabel.RIGHT));
		this.add(this.username);
		this.add(new JLabel("Events:  ", JLabel.RIGHT));
		this.add(this.messages);
		this.add(new JLabel(""));
		this.add(this.loginout);

		// add action to the button
		this.loginout.setActionCommand("logout");
		this.loginout.addActionListener(this);
	}

	/**
	 * Set the nickname JLabel on the stat panel.
	 */
	private void updateUserName() {
		String newnick = smack.getNickname();
		if (newnick != null) {
			this.username.setText(newnick);
			this.messages.setText("0");
			this.setSentMessages(0);
		} else {
			this.username.setText("not logged in");

		}
	}

	/**
	 * Increment the XMPP message counter.
	 */
	public void incrementMessages() {
		this.sentMessages = this.sentMessages + 1;
		this.updateSentMessages();
	}

	/**
	 * Update the XMPP message counter JLabel.
	 */
	public void updateSentMessages() {
		this.messages.setText(Integer.toString(this.sentMessages));
	}

	/**
	 * Set the "Login/Logout" button on the panel.
	 */
	private void updateLogInOut() {

		if (smack.isLoggedin()) {
			this.loginout.setText("Logout");
		} else {
			this.loginout.setText("Login");

		}
	}

	public void actionPerformed(ActionEvent e) {
		if ("logout".equals(e.getActionCommand())) {
			if (smack.isLoggedin()) {
				smack.disconnect();
				relogin();
			} else {
				relogin();
			}
		}
	}

	/**
	 * The method launches the XMPP login dialog window so
	 * that the user can relaunch the client.
	 */
	public void relogin() {
		// Attempt to Login, do not start if failed.
		if (!smack.loginWindow()) {
			System.exit(0);
		} else { // join the room
			if (!smack.joinMUC(ini.getXmppTargetRoom())) {
				System.err.println();
				JOptionPane.showMessageDialog(null, "Cannot Join chat room: "
						+ ini.getXmppTargetRoom(), "Server Error",
						JOptionPane.ERROR_MESSAGE);

			}
			smack.refreshGUIcomponents();

		}
	}

	/**
	 * Get method.  Return the number of messages sent.
	 * @return the sentMessages
	 */
	public int getSentMessages() {
		return sentMessages;
	}

	/**
	 * Set the number of messages sent.
	 * @param sentMessages
	 *            the sentMessages to set
	 */
	public void setSentMessages(int sentMessages) {
		this.sentMessages = sentMessages;
	}
}
